﻿using DeviceFaultPushService.DeviceProcessManagement.CommunicationManagement.ModbusTcp.Models;
using DeviceFaultPushService.MessagePushService.Models;

namespace DeviceFaultPushService.DeviceProcessManagement.CommunicationManagement.ModbusTcp;

public class ConfigurationGeneration
{
    private DeviceInformationModel _deviceInformation = new();

    public DeviceInformationModel deviceInformation
    {
        get
        {
            return _deviceInformation;
        }
    }

    public void GenerationDeviceInformation(string number, string name, string protocol)
    {
        _deviceInformation.Number = number;
        _deviceInformation.Name = name;
        _deviceInformation.Protocol = (CommunicationManagement.Models.ProtocolEnum)Enum.Parse(typeof(CommunicationManagement.Models.ProtocolEnum), protocol, true);
    }

    public void GenerationDeviceAddress(string ip, string port)
    {
        _deviceInformation.DeviceAddress = new DeviceAddressModel()
        {
            Ip = ip,
            Port = short.Parse(port),
        };
    }

    public void GenerationContacts(string phoneNumber, string level, string enableDialing, string enableSms)
    {
        if (_deviceInformation.Contacts == null)
        {
            _deviceInformation.Contacts = new List<ContactsModel>();
        }
        _deviceInformation.Contacts.Add(new ContactsModel()
        {
            PhoneNumber = phoneNumber,
            UserLevel = (UserLevelEnum)Enum.Parse(typeof(UserLevelEnum), level, true),
            EnableSms = bool.Parse(enableSms),
            EnableDialing = bool.Parse(enableDialing)
        });
    }

    public void GenerationDataAddress(string functionCode, string slaveAddress, string startAddress, string numberOfPoints)
    {
        if (_deviceInformation.DataAddress == null)
        {
            _deviceInformation.DataAddress = new();
        }
        _deviceInformation.DataAddress.FunctionCode = (FunctionBlockEnum)Enum.Parse(typeof(FunctionBlockEnum), functionCode, true);
        _deviceInformation.DataAddress.SlaveAddress = byte.Parse(slaveAddress);
        _deviceInformation.DataAddress.StartAddress = ushort.Parse(startAddress);
        _deviceInformation.DataAddress.NumberOfPoints = ushort.Parse(numberOfPoints);
    }

    public void GenerationDataAddress(List<bool> isEnable, List<string> alarmInformation)
    {
        if (_deviceInformation.DataAddress == null)
        {
            _deviceInformation.DataAddress = new();
        }
        _deviceInformation.DataAddress.IsEnable = isEnable;
        _deviceInformation.DataAddress.AlarmInformation = alarmInformation;

        if (_deviceInformation.DataAddress.NumberOfPoints != isEnable.Count)
        {
            throw new("数据添加错误，报警状态数量与报警信息数量不一致");
        }
        if (isEnable.Count != alarmInformation.Count)
        {
            throw new("数据添加错误，报警状态数量与报警信息数量不一致");
        }

        List<string> uid = new List<string>();
        foreach (var VARIABLE in isEnable)
        {
            var uuidN = Guid.NewGuid().ToString("N");

            if (uid.Contains(uuidN) == false)
            {
                uid.Add(uuidN);
            }
            else
            {
                uid.Add(DateTimeOffset.Now.ToUnixTimeSeconds().ToString());
            }
        }
        _deviceInformation.DataAddress.Uid = uid;
    }
}

